package com.dgut.coal.controller;

import com.dgut.coal.entity.User;
import com.dgut.coal.service.UserService;
import com.dgut.coal.util.PasswordMD5Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Yang
 * @Description:
 * @Date: Created in 23:42 2019/9/27
 * @Modifued BY:
 */
@Controller

public class UserController {

    @Autowired
    UserService userService;


    /**
     * 前往个人信息页面
     */
    @RequestMapping("/adminmessage")
    public String message(){
        return "admin-message1";
    }

    /**
     * 前往企业信息页面
     */
    @RequestMapping("/companyMessage")
    public String company(){
        return "admin-company";
    }


    //用户信息
    @RequestMapping("/findAll")
    public ModelAndView findAll(@RequestParam(name="page",required = true,defaultValue = "1")int page, @RequestParam(name="size",required = true,defaultValue = "4")int size){

        ModelAndView mv=new ModelAndView();
        List<User> users = userService.findAll(page,size);

        //pageHelper分页功能
        PageInfo pageinfo=new PageInfo(users);
        mv.addObject("pageinfo",pageinfo);
        mv.setViewName("admin-list");
        return mv;
    }


    //按条件查找用户列表,
    @RequestMapping("/findByConditions")
    public ModelAndView findByConditions(User u, @RequestParam(required = false,defaultValue = "1",value = "page") Integer pageNum){

        PageHelper.startPage(pageNum,5);
        List<User> users=userService.findByConditions(u);
        PageInfo pageInfo = new PageInfo<>(users,5);

        List list = Arrays.asList(u.getUser_id(),u.getUsername(),u.getRole_type());
        ModelAndView mv=new ModelAndView();
        mv.addObject("users",users);
        mv.addObject("keywords",list);
        mv.addObject("pageinfo",pageInfo);
        mv.setViewName("admin-list-conditions");

        return mv;
    }


    @RequestMapping("/adminAdd")
    public String adminAdd(){
        return "admin-add";
    }

    //添加用户
    @RequestMapping("/saveUser")
    public ModelAndView saveUser(User u){

        Map<String,String> errors=new HashMap<>();
        u.setPassword(PasswordMD5Utils.getMD5String(u.getPassword()));
        //验证邮箱格式
        String EMAIL_REGEX = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";
        if(!u.getEmail().matches(EMAIL_REGEX)){
            errors.put("email","邮箱格式不正确");
        }
        //验证电话格式
        String PHONE_REGEX = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9])|(16[6]))\\d{8}$";
        if(!u.getMobile().matches(PHONE_REGEX)){
            errors.put("mobile","请输入正确的号码");
        }

        ModelAndView mv=new ModelAndView();
        mv.addObject("auser",u);
        //如何有错误，即表示邮箱或者电话格式不对，携带user,errors(回显信息),返回admin-add.jsp
        if(errors.size()>0){
            mv.addObject("errors",errors);
            mv.setViewName("admin-add");
            return mv;
        }else{
            userService.saveUser(u);
        }
        //验证通过，保存后调用findAll()显示所有账户
        return findAll(1,4);
    }

    //删除用户
    @RequestMapping("/deleteUser")
    public ModelAndView deleteUser(Integer id){
        userService.deleteUser(id);
        return findAll(1,4);
    }

    //编辑前回显账户信息
    @RequestMapping("/preEdit")
    public ModelAndView preEdit(Integer id){


        System.out.println(id);
        User user=userService.findById(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("user",user);
        mv.setViewName("admin-edit");

        return mv;
    }

    //编辑账户信息
    @RequestMapping("/editUser")
    public ModelAndView editUser(User u){

        System.out.println(u);

        userService.updateUser(u);

        return findAll(1,4);
    }

    @RequestMapping("/editpassword")
    public ModelAndView editpassword(Integer id){

        ModelAndView mv=new ModelAndView();
        mv.addObject("id",id);
        mv.setViewName("admin-editpassword");

        return mv;
    }


    /**
     * 修改密码
     */

    @RequestMapping("/updatePwd")

    public ModelAndView updatePwd(Integer user_id,String pwd,String new_pwd,String renew_pwd){
        /**
        ** user_id：通过账户ID查找出密码，与输入的密码pwd进行对比
        ** new_pwd:新密码
        ** renew_pwd:重新输入密码
         */
        User user=userService.findById(user_id);
        Map<String,String> errors=new HashMap<>();

        if(!user.getPassword().equals(PasswordMD5Utils.getMD5String(pwd))){
            errors.put("pwd","密码错误");
        }
        if(!new_pwd.equals(renew_pwd)){
            errors.put("new_pwd","2次密码不一致");
        }
        System.out.println(errors);
        ModelAndView mv=new ModelAndView();
        User u=new User();
        u.setUser_id(user_id);
        u.setPassword(PasswordMD5Utils.getMD5String(new_pwd));

        if(errors.size()>0){
            mv.addObject("errors",errors);
            mv.addObject("id",user_id);
            mv.setViewName("admin-editpassword");
        }else{
            userService.updatePwd(u);
            mv.setViewName("admin-message1");
        }

        return mv;
    }

    //查看管理员信息
    @RequestMapping("/show")
    public ModelAndView show(Integer id){

        User u=userService.findById(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("user",u);
        mv.setViewName("admin-view");
        return mv;
    }


}
